<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      :icon-class=" !isFullScreen ? 'fullscreen' : 'exit-fullscreen'"
      class="fullscreen"
      @click="toogleScreen"
    />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false
    }
  },
  created() {
    // ESC会改变当前的全屏状态
    // screenfull插件自带的事件监听on
    // screenfull.isFullscreen：插件自带的属性，表示当前是否处于全屏状态
    screenfull.on('change', () => {
      console.log('当前全屏状态', screenfull.isFullscreen)
      this.isFullScreen = screenfull.isFullscreen
    })
  },
  methods: {
    toogleScreen() {
      // 调用插件的全屏切换功能
      screenfull.toggle()
      this.isFullScreen = !this.isFullScreen
    }
    // 浏览器底层给我们提供了开启全屏和关闭全屏的API方法，有兼容性问题
    // toogleScreen() {
    //   if (this.isFullScreen === true) {
    //     // 全屏
    //     document.documentElement.webkitRequestFullScreen()
    //   } else {
    //     document.exitFullscreen()
    //   }
    //   this.isFullScreen = !this.isFullScreen
    // }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
